package com.njtoyo.taxi.admin.rest.controller.business.operation_rule;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.StyleSet;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.njtoyo.taxi.admin.mapper.master.entity.PrivateCallApplyRecord;
import com.njtoyo.taxi.admin.rest.presenter.business.operation_rule.privacy_number_report.PrivacyNumberReportPagePresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.operation_rule.online_valuation.OnlineValuationAddUpdateWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.operation_rule.privacy_number_report.PrivacyNumberReportAddUpdateWrapper;
import com.njtoyo.taxi.admin.service.business.operation_rule.OnlineValuationRuleService;
import com.njtoyo.taxi.admin.service.business.operation_rule.PrivacyNumberReportService;
import com.njtoyo.taxi.entity.backend.AdminUser;
import com.taxi.entity.common.RestResult;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.CellStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("business/operation-rule/privacy-number-report")
public class PrivacyNumberReportController {


    @Autowired
    PrivacyNumberReportService privacyNumberReportService;


    @RequestMapping("/page")
    public RestResult page(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                           @RequestParam(value = "size", required = false, defaultValue = "15") Integer size,
                           @RequestParam Map<String, String> params) {
        IPage<PrivacyNumberReportPagePresenter> page = privacyNumberReportService.page(current, size, params);
        return RestResult.success(page);
    }


    /**
     * 统计 在职司机数、提交、未提交
     *
     * @return
     */
    @RequestMapping("/total")
    public RestResult total() {
        return privacyNumberReportService.total();
    }


    @RequestMapping("/unApplyPage")
    public RestResult unApplyPage(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                                  @RequestParam(value = "size", required = false, defaultValue = "15") Integer size,
                                  @RequestParam Map<String, String> params) {
        IPage<PrivacyNumberReportPagePresenter> page = privacyNumberReportService.unApplyPage(current, size, params);
        return RestResult.success(page);
    }

    @SneakyThrows
    @RequestMapping("/unApply/export")
    public void unApplyExport(HttpServletResponse response, @RequestParam Map<String, String> params) {
        IPage<PrivacyNumberReportPagePresenter> page = privacyNumberReportService.unApplyPage(1, 100000, params);
        List<Map<String, Object>> rows = new ArrayList<>();
        for (PrivacyNumberReportPagePresenter privacyNumberReport : page.getRecords()) {
            Map<String, Object> row = new HashMap<>();
            row.put("司机ID", privacyNumberReport.getDriverId());
            row.put("司机手机号", privacyNumberReport.getMobileNumber());
            row.put("司机姓名", privacyNumberReport.getDriverName());
            rows.add(row);
        }

        ExcelWriter writer = ExcelUtil.getWriter();
        StyleSet style = writer.getStyleSet();
        CellStyle cellStyle = style.getHeadCellStyle();
        cellStyle.setWrapText(true);
        writer.write(rows, true);
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=cars.xls");
        ServletOutputStream out = response.getOutputStream();

        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
    }


    @PostMapping("/add")
    public RestResult add(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser, @Validated @RequestBody PrivacyNumberReportAddUpdateWrapper wrapper) {
        Boolean phoneExist = checkPhoneNumberExist(null, wrapper.getPhoneNumber());
        if (phoneExist) {
            return RestResult.failed("该号码已经报备过了");
        }
        return privacyNumberReportService.add(wrapper, adminUser);
    }

    @GetMapping("/detail/{id}")
    public RestResult detail(@PathVariable("id") String id) {
        return privacyNumberReportService.getDetailById(id);
    }

    @PostMapping("/update")
    public RestResult update(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser, @Validated @RequestBody PrivacyNumberReportAddUpdateWrapper wrapper) {
        Boolean phoneExist = checkPhoneNumberExist(wrapper.getId(), wrapper.getPhoneNumber());
        if (phoneExist) {
            return RestResult.failed("该号码已经报备过了");
        }
        return privacyNumberReportService.update(wrapper, adminUser);
    }

    private Boolean checkPhoneNumberExist(Long id, String phoneNumber) {

        PrivateCallApplyRecord record = privacyNumberReportService.selectByPhoneNumber(id, phoneNumber);
        return ObjectUtil.isNotNull(record);
    }

}
